package cn.itlight.filter;

import com.alibaba.fastjson.JSON;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;

import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Component
public class CustomFilter extends ZuulFilter {

    /*
     * @Title: filterType
     * @Description: zuul的返回类型
     * @Author: TomLiu
     * @Version: 1.0
     * @Date:  2022/7/31 10:59
     * @Parameters:
     * @Return
     */
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    /*
     * @Title: filterOrder
     * @Description: 数字越小优先级越高
     * @Author: TomLiu
     * @Version: 1.0
     * @Date:  2022/7/31 11:00
     * @Parameters:
     * @Return
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /*
     * @Title: shouldFilter
     * @Description: 要不要执行run方法，true就执行
     * @Author: TomLiu
     * @Version: 1.0
     * @Date:  2022/7/31 11:00
     * @Parameters:
     * @Return
     */
    @Override
    public boolean shouldFilter() {
        HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
        String requestURI = request.getRequestURI();
        if (requestURI.endsWith("/register") || requestURI.endsWith("/login") || requestURI.contains("/static/")){
            return false;
        }


        return true;
    }

    /*
     * @Title: run
     * @Description: 核心方法
     * @Author: TomLiu
     * @Version: 1.0
     * @Date:  2022/7/31 11:01
     * @Parameters:
     * @Return
     */
    @Override
    public Object run() throws ZuulException {

        //1.获取请求对象
        HttpServletRequest request = RequestContext.getCurrentContext().getRequest();

        //响应对象
        HttpServletResponse response = RequestContext.getCurrentContext().getResponse();

        //2.获取请求头中的 token
        String token = request.getHeader("token");

        // 判断token
        if (StringUtils.isBlank(token) && !isToken(token)){
            Map<String,Object> resultMap = new HashMap<>();
            resultMap.put("success" , false);
            resultMap.put("message" , "没有登录，请重新登录");
            resultMap.put("code" , 402);
            resultMap.put("data" , null);
            //中文编码
            response.setContentType("application/json;charset=utf-8");
            String resultJsonString = JSON.toJSONString(resultMap);
            response.setStatus(402);
            try {
                response.getWriter().print(resultJsonString);
            } catch (IOException e) {
                e.printStackTrace();
            }

            //3.2.阻止filter继续往后执行
            RequestContext.getCurrentContext().setSendZuulResponse(false);

        }

        return null;
    }

    private boolean isToken(String token){
        // todo
        return false;
    }
}
